package cn.kgc.hotel.controller;

import cn.kgc.hotel.entity.Food;
import cn.kgc.hotel.entity.FoodReserve;
import cn.kgc.hotel.entity.ReserveFoodNum;
import cn.kgc.hotel.service.FoodService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/food")
public class FoodController {
    @Resource
    private FoodService foodService;

    //查询所有餐品
    @RequestMapping("/searchFoods")
    List<Food> searchFoods() {
        List<Food> foodList = foodService.searchFoods();
        for (Food food : foodList) {
            System.out.println(food.getFoodName());
        }
        return foodList;
    }

    //餐品预订
    @RequestMapping("/foodReserve")
    Boolean foodReserve(@RequestBody String data) {
        System.out.println("data:" + data);
        //将字符串转为JSON对象
        JSONObject jsonObject = JSON.parseObject(data);
        //将JSON对象的属性转为相应的java对象FoodReserve
        FoodReserve foodReserve = jsonObject.getObject("foodReserve", FoodReserve.class);
        //获取当前系统时间，按格式转为字符串并赋值给属性
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        foodReserve.setTime(timeFormat.format(date));
        //生成订单ID第一步：获取时间戳
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String reserveId = sdf.format(date);
        //生成订单ID第二步：追加6位随机数字
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            reserveId += random.nextInt(10);
        }
        //生成订单ID第三步：追加用户ID,生成最终的订单ID
        reserveId += foodReserve.getUserId();
        foodReserve.setReserveId(reserveId);

        //设置foodReserve的其他属性
        foodReserve.setOnline(1);//通过该控制器预订则必为线上预订，值为1
        foodReserve.setStateId(1);//提交订单后默认为待审核状态，值为1


        //将JSON对象的属性转为数组，将其元素转为java对象并添加至List
        JSONArray reserveFoodNumArray = jsonObject.getJSONArray("reserveFoodNumList");
        List<ReserveFoodNum> reserveFoodNumList = new ArrayList<>();
        for (int i = 0; i < reserveFoodNumArray.size(); i++) {
            ReserveFoodNum reserveFoodNum = reserveFoodNumArray.getObject(i, ReserveFoodNum.class);
            //给reserveID赋值
            reserveFoodNum.setReserveId(reserveId);

            reserveFoodNumList.add(reserveFoodNum);
        }
        System.out.println("AAAA" + foodReserve.getReserveId());
        System.out.println("BBBBB" + reserveFoodNumList.get(0).getFoodId());

        boolean flag = foodService.foodReserve(foodReserve, reserveFoodNumList);
        return flag;
    }

    //查询餐品预订.
    @RequestMapping("/searchFoodReserve")
    List<FoodReserve> searchFoodReserve(int userId){
        List<FoodReserve> foodReserveList = foodService.searchFoodReserve(userId);
        return foodReserveList;
    }

    //取消餐品预订
    @RequestMapping("/cancelFoodReserve")
    Boolean cancelFoodReserve(String reserveId){
        boolean flag = foodService.cancelFoodReserve(reserveId);
        return flag;
    }
}
